package org.seckill.web;

import java.util.Date;
import java.util.List;

import org.seckill.dto.Exposer;
import org.seckill.dto.SeckillExecution;
import org.seckill.dto.SeckillResult;
import org.seckill.entity.Seckill;
import org.seckill.enums.SeckillStateEnum;
import org.seckill.exception.RepeatKillException;
import org.seckill.exception.SeckillCloseException;
import org.seckill.exception.SeckillException;
import org.seckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/seckill")
public class SeckillController {
	@Autowired
	SeckillService seckillService;

	@RequestMapping(value = "/list" , method = RequestMethod.GET)
	public String list(Model model){
		List<Seckill> list = seckillService.getSeckillList();
		model.addAttribute("list", list);
		model.addAttribute("a", "a");
		//ViewAndResolver = list.jsp + model
		return "list";
	}

	@RequestMapping(value="/{seckillId}/detail", method = RequestMethod.GET)
	public String detail(@PathVariable("seckillId")Long seckillId, Model model){
		if(seckillId == null){
			return "redirect:/seckill/list";
		}

		Seckill seckill = seckillService.getSeckillById(seckillId);
		if(seckill == null){
			return "forward:/seckill/list";
		}

		model.addAttribute("seckill", seckill);
		return "detail";
	}

	//ajax json  	@ResponseBody:表示返回的方式    produces标注返回的是json
	@RequestMapping(value="/{seckillId}/exposer",
			method=RequestMethod.POST,
			produces = "application/json;charset=utf-8")
	@ResponseBody
	public SeckillResult<Exposer> exposer(Long seckillId){
		SeckillResult<Exposer> result;
		try {
			Exposer exposer = seckillService.exportSeckillUrl(seckillId);
			result = new SeckillResult<Exposer>(true, exposer);
		} catch (Exception e) {
			e.printStackTrace();
			result = new SeckillResult<Exposer>(true, e.getMessage());
		}
		return result;
	}

	@RequestMapping(value="/{seckillId}/{md5}/execute",
			method=RequestMethod.POST,
			produces="application/json;charset=utf-8")
	@ResponseBody
	public SeckillResult<SeckillExecution> execute(@PathVariable("seckillId")Long seckillId,
			@PathVariable("md5")String md5, @CookieValue(value="killPhone", required=false)Long userPhone){
		if(userPhone == null){
			return new SeckillResult<SeckillExecution>(false, "未注册");
		}
		try {
			//执行秒杀
			SeckillExecution execution = seckillService.executeSeckill(seckillId, userPhone, md5);
			return  new SeckillResult<SeckillExecution>(true, execution);
		} catch (RepeatKillException e) {
			SeckillExecution execution = new SeckillExecution(seckillId, SeckillStateEnum.REPEAT_KILL);
			return  new SeckillResult<SeckillExecution>(false, execution);
		} catch (SeckillCloseException e) {
			SeckillExecution execution = new SeckillExecution(seckillId, SeckillStateEnum.END);
			return  new SeckillResult<SeckillExecution>(false, execution);
		} catch (SeckillException e) {
			SeckillExecution execution = new SeckillExecution(seckillId, SeckillStateEnum.INNER_ERROR);
			return  new SeckillResult<SeckillExecution>(false, execution);
		}
	}

	@RequestMapping(value="/{seckillId}/{md5}/execute",method=RequestMethod.POST)
	public SeckillResult<Date> time(){
		Date now = new Date();
		return new SeckillResult<Date>(true, now);
	}
}
